	clear all
	set more off

	use "$data/choice_data.dta", clear

	keep ID risk_G_J-risk_I_N
	
	******************************************************
	***			CONSISTENCY IN RISK TASK			   ***
	******************************************************

	* (0,9) chosen over (3,3) and (3,3) chosen over (0,15)
	
	gen risk_consistency1 = 0			if	 risk_G_L < .	& risk_G_J < . 
		replace risk_consistency1 = 1	if 	(risk_G_L == 6 	| risk_G_L == 15) & (risk_G_J == 1 	| risk_G_J == 15)
		

	* (3,3) chosen over (0,9) and (0,9) chosen over (6,6)
	
	gen risk_consistency2 = 0			if	 risk_G_L < .	& risk_H_L < . 
		replace risk_consistency2 = 1	if 	(risk_G_L == 1 	| risk_G_L == 15) & (risk_H_L == 6 	| risk_H_L == 15)

		
	* (0,9) chosen over (6,6) and (6,6) chosen over (0,15) 
			
	gen risk_consistency3 = 0			if	 risk_H_L <  .	& risk_H_J < . 	 
		replace risk_consistency3 = 1	if 	(risk_H_L == 6 	| risk_H_L == 15) & (risk_H_J == 2 	| risk_H_J == 15)

		
	* (3,3) chosen over (0,15) and (0,15) chosen over (6,6) 
			
	gen risk_consistency4 = 0			if	 risk_G_J <  .	& risk_H_J < . 	 
		replace risk_consistency4 = 1	if 	(risk_G_J == 1 	| risk_G_J == 15) & (risk_H_J == 4 	| risk_H_J == 15)
		
			
	* (3,3) chosen over (0,15) and (0,15) chosen over (9,9)
			
	gen risk_consistency5 = 0			if	 risk_G_J <  .	& risk_I_J < . 	 
		replace risk_consistency5 = 1	if 	(risk_G_J == 1 	| risk_G_J == 15) & (risk_I_J == 4 	| risk_I_J == 15) 
		

	* (6,6) chosen over (0,15) and (0,15) chosen over (9,9)
			
	gen risk_consistency6 = 0			if	 risk_H_J <  .	& risk_I_J < . 	 
		replace risk_consistency6 = 1	if 	(risk_H_J == 2 	| risk_H_J == 15) & (risk_I_J == 4 	| risk_I_J == 15)

		
	* (0,9) chosen over (6,6) and (6,6) chosen over (3,12)
			
	gen risk_consistency7 = 0			if	 risk_H_L <  .	& risk_H_K < . 	 
		replace risk_consistency7 = 1	if 	(risk_H_L == 6 	| risk_H_L == 15) & (risk_H_K ~= 5  & risk_H_K < .)


	* (3,12) chosen over (9,9) and (6,6) chosen over (3,12)
			
	gen risk_consistency8 = 0			if	 risk_I_K <  .	& risk_H_K < . 	 
		replace risk_consistency8 = 1	if 	(risk_I_K == 5 	| risk_I_K == 15) & (risk_H_K ~= 5  & risk_H_K < .)
	
	
	
	egen risk_consistency = rowtotal(risk_consistency*)
	
	******************************************************	
	
	preserve
	
		keep if risk_consistency == 0
		
		keep ID risk_consistency
	
		gen ndrops = 0
	
		tempfile sample1
		
		save `sample1'
	
	restore
		
	drop if risk_consistency == 0
	
	count
	
	******************************************************	
		
	
	cap program drop count_drops
	program define count_drops
	
	local all_decisions "risk_G_J risk_H_J risk_H_L risk_G_L risk_H_K risk_I_J risk_I_K"
	
	local risk_G_J "1 4 5"	
	
	local risk_G_L "1 2"
	
	local risk_H_J "3 4 6"
	
	local risk_H_K "7 8"
	
	local risk_H_L "2 3 7"
	
	local risk_I_J "5 6" 
	
	local risk_I_K "8"
	
	
	
	preserve
	
	qui keep if id == `1'
	
	local inconsistencies = ""
	
	forvalues i = 1/8 {
				
		qui summ risk_consistency`i'
		
		if r(mean) == 1 {
			
			local inconsistencies = "`inconsistencies' `i'"
			
		}
				
	}		
	
	
	local n_inconsistencies : word count `inconsistencies'

	
	*** drop 1
	
	foreach decision_to_drop1 in `all_decisions' {
		
			local inconsistencies_m1: list inconsistencies - `decision_to_drop1'
			
			local n_inconsistencies : word count `inconsistencies_m1'
			
		if `n_inconsistencies' == 0 {

			global ndrops = 1
		
			continue, break
		
		}
		
	}
	*/
	
	*** drop 2
	
	if `n_inconsistencies' > 0 {
	
		foreach decision_to_drop1 in risk_G_J risk_H_J risk_H_L risk_G_L risk_H_K risk_I_J {
			
			local other_decisions1: list all_decisions - decision_to_drop1
		
			foreach decision_to_drop2 in `other_decisions1' {
			
				local inconsistencies_m2: list inconsistencies - `decision_to_drop1' 

				local inconsistencies_m2: list inconsistencies_m2 - `decision_to_drop2'

				local n_inconsistencies : word count `inconsistencies_m2'
				
				if `n_inconsistencies' == 0 {

				continue, break
			
				}	
				
			}
			
			if `n_inconsistencies' == 0 {

				global ndrops = 2
			
				continue, break
				
			}
			
		}
		
	}
	*/
		
	*** drop 3

	
	if `n_inconsistencies' > 0 {
		
		foreach decision_to_drop1 in risk_G_J risk_H_J risk_H_L risk_G_L risk_H_K {
			
			local other_decisions1: list all_decisions - decision_to_drop1

			foreach decision_to_drop2 in `other_decisions1' {
			
				local other_decisions2: list other_decisions1 - decision_to_drop2
			
				foreach decision_to_drop3 in `other_decisions2' {	
			
					local inconsistencies_m3: list inconsistencies - `decision_to_drop1' 

					local inconsistencies_m3: list inconsistencies_m3 - `decision_to_drop2'

					local inconsistencies_m3: list inconsistencies_m3 - `decision_to_drop3'

					local n_inconsistencies : word count `inconsistencies_m3'
					
					if `n_inconsistencies' == 0 {

					continue, break
				
					}
				}
				
				if `n_inconsistencies' == 0 {

					continue, break
				
				}
				
				
			}
			
			if `n_inconsistencies' == 0 {

				global ndrops = 3
			
				continue, break
				
			}
			
		}
		
	}
	
	*dis `n_inconsistencies'
	
	*/
	
	*** drop 4
	
	if `n_inconsistencies' > 0 {
		
		foreach decision_to_drop1 in risk_G_J risk_H_J risk_H_L risk_G_L risk_H_K {
			
			local other_decisions1: list all_decisions - decision_to_drop1

			foreach decision_to_drop2 in `other_decisions1' {
			
				local other_decisions2: list other_decisions1 - decision_to_drop2
			
				foreach decision_to_drop3 in `other_decisions2' {	
			
					local other_decisions3: list other_decisions2 - decision_to_drop3
				
					foreach decision_to_drop4 in `other_decisions3' {	
				
						local inconsistencies_m4: list inconsistencies - `decision_to_drop1' 

						local inconsistencies_m4: list inconsistencies_m4 - `decision_to_drop2'

						local inconsistencies_m4: list inconsistencies_m4 - `decision_to_drop3'

						local inconsistencies_m4: list inconsistencies_m4 - `decision_to_drop4'

						local n_inconsistencies : word count `inconsistencies_m4'
						
						if `n_inconsistencies' == 0 {

						continue, break
					
						}
					}
				
					if `n_inconsistencies' == 0 {

						continue, break
					
					}
					
				}
				
				if `n_inconsistencies' == 0 {

					continue, break
				
				}
				
			}
			
			if `n_inconsistencies' == 0 {

				global ndrops = 4
			
				continue, break
				
			}
			
		}	
	
	}
	
	*/
	
	*** drop 5
	
	if `n_inconsistencies' > 0 {
		
		foreach decision_to_drop1 in risk_G_J risk_H_J risk_H_L risk_G_L risk_H_K {
			
			local other_decisions1: list all_decisions - decision_to_drop1

			foreach decision_to_drop2 in `other_decisions1' {
			
				local other_decisions2: list other_decisions1 - decision_to_drop2
			
				foreach decision_to_drop3 in `other_decisions2' {	
			
					local other_decisions3: list other_decisions2 - decision_to_drop3
				
					foreach decision_to_drop4 in `other_decisions3' {	
				
						local other_decisions4: list other_decisions3 - decision_to_drop4
					
						foreach decision_to_drop5 in `other_decisions4' {	
					
							local inconsistencies_m5: list inconsistencies - `decision_to_drop1' 

							local inconsistencies_m5: list inconsistencies_m5 - `decision_to_drop2'

							local inconsistencies_m5: list inconsistencies_m5 - `decision_to_drop3'

							local inconsistencies_m5: list inconsistencies_m5 - `decision_to_drop4'

							local inconsistencies_m5: list inconsistencies_m5 - `decision_to_drop5'
							
							local n_inconsistencies : word count `inconsistencies_m5'
							
							
							if `n_inconsistencies' == 0 {

							continue, break
						
							}
						}
					
						if `n_inconsistencies' == 0 {

							continue, break
						
						}
						
					}
					
					if `n_inconsistencies' == 0 {

						continue, break
					
					}		
						
				}
				
				if `n_inconsistencies' == 0 {

					continue, break
				
				}
				
			}
			
			if `n_inconsistencies' == 0 {

				global ndrops = 5
			
				continue, break
				
			}
			
		}	
	
	}
	
	restore
	
	end
	
	
	
	egen id = group(ID)
	
	summ id
	local N = r(max)
		
	gen ndrops = .
	
	
	forvalues id = 1/`N' {
		
		count_drops `id'
		qui replace ndrops = $ndrops	if id == `id'
		
	}
	
	
	append using `sample1'
	
	
	
	gen hm_risk = 8 - ndrops	
	
	keep ID hm_risk
	
	
	sort ID
	save "$data/secondary/Houtman and Maks/HM_risk.dta", replace
